var url = require("url")
var mysql = require("mysql")
var express = require("express")()
var port = 8085
var n = 1



var sql = mysql.createConnection({
	host     :"localhost",
	user     :"root",
	password :"469151491",
	database :"zs2"
})
sql.connect()



express.all("/*", function(req, res, next) {
    // 跨域处理
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next(); // 执行下一个路由
})

//服务员进行登录
express.get("/admin",function(request,response){
	var parse = url.parse(request.url,true).query
	var username = parse.username
	var password = parse.password
	sql.query(`select * from adminuser where username="${username}" and password="${password}"`,function(error,result){
		if(error){
			console.log(error)
		}
		else{
			if(result.length){
				console.log(`用户${result[0].username}登录成功！`)
				response.end(JSON.stringify(result))
			}
			if(result.length ==0){
				console.log(`用户登录失败！`)
				response.end(JSON.stringify(result.length))
			}
		}
	})
})

//手机搜索关键字查询菜品
express.get("/getsearch",(request,response)=>{
	var search=url.parse(request.url,true).query.search
	sql.query(`select * from foodname where search="${search}"`,(error,data)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end(JSON.stringify(data))
		}
	})
})

//查询桌号数据
express.get("/setlist",function(request,response){
	sql.query(`select * from setlist`,function(error,result){
		response.end(JSON.stringify(result))
		if(error){
			console.log(error)
		}
	})
})


//7小时计时器
setInterval(function(){
	sql.query(`select * from foodname`,(error,result)=>{
		if(error){
			console.log(error)
		}
	})
},25200000)



//获取所有菜品名称
express.get("/getfoodname",(request,response)=>{
	sql.query(`select * from foodname`,(error,result)=>{
		response.end(JSON.stringify(result))
		if(error){
			console.log(error)
		}
	})
})

//查询历史未结账订单
express.get("/gethistory",(request,response)=>{
	var setid = url.parse(request.url,true).query.setid
	sql.query(`select * from sumlist where setid=${setid} and status=0`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end(JSON.stringify(result))
		}
	})
})

//提交订单之前判断价格是否相等
express.get("/getlength2",(request,response)=>{
	var setid = url.parse(request.url,true).query.setid
	var sumprice = url.parse(request.url,true).query.sum
	sql.query(`select * from sumlist where setid=${setid} and sumprice=${sumprice} and status=0`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${setid}号订单价格查询成功！`)
			response.end(JSON.stringify(result))
		}
	})
})

//提交订单之前判断历史订单与此刻提交时的订单长度相比是否相等
express.get("/getlength",(request,response)=>{
	var setid = url.parse(request.url,true).query.setid
	sql.query(`select * from sumlist where setid=${setid} and status=0`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${setid}号订单数量查询成功！`)
			response.end(JSON.stringify(result))
		}
	})
})

//点餐数据上传
express.get("/upload",(request,response)=>{
	var parse = url.parse(request.url,true).query
	var setid = parse.setid
	var foodlist = parse.list
	var sumprice = parse.sumprice
	var settime = parse.settime
	var insertsql= `insert into sumlist (setid,foodlist,sumprice,settime) values (${setid},'${foodlist}',${sumprice},"${settime}")`
	var selectsql=`select * from sumlist where setid=${setid} and status=0`
	var updatesql=`update sumlist set foodlist='${foodlist}',sumprice=${sumprice} where setid=${setid} and status=0`
	sql.query(selectsql,(error,data)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			if(data.length>0){
				sql.query(updatesql,(error,data)=>{
					if(error){
						console.log(error)
						response.end("error")
					}
					else{
						console.log(`${setid}号桌菜品订单更新成功！`)
						response.end("success!")
					}
				})
			}
			else{
				sql.query(insertsql,function(error,data){
					if(error){
						console.log(error)
						response.end("error")
						console.log(`添加${setid}号桌的数据失败！`)
					}
					else{
						response.end("success!")
						console.log(`添加${setid}号桌的数据成功！`)
					}
				})
			}
		}
	})
})

//订单提交成功后更新桌号状态
express.get("/change",(request,response)=>{
	var setid=url.parse(request.url,true).query.setid
	sql.query(`update setlist set status=1 where setid=${setid} and status=0`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end("success")
		}
	})
})

//前台获取桌号状态数据
express.get("/getstatus",(request,response)=>{
	sql.query(`select setlist.setid,setlist.status,sumlist.sumprice,sumlist.settime from setlist left join sumlist on setlist.setid=sumlist.setid and sumlist.status=0`,(error,data)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end(JSON.stringify(data))
		}
		
	})
})

//强制设置桌位为空闲状态
express.get("/reset",(request,response)=>{
	var setid=url.parse(request.url,true).query.setid
	var statussql=`update setlist set status=0 where setid=${setid} and status=1` 
	var statussql2=`update sumlist set status=1 where setid=${setid} and status=0`
	sql.query(statussql,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${setid}号桌强制设置为空闲状态的操作成功！`)
			response.end("success")
		}
	})
	sql.query(statussql2,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${setid}号桌未付账，已强制设置为空闲状态的操作成功！`)
			response.end("success")
		}
	})
})

//前台获取订单
express.get("/getorder",(request,response)=>{
	var setid=url.parse(request.url,true).query.setid
	sql.query(`select * from sumlist where setid=${setid} and status=0`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`获取${setid}号桌订单成功！`)
			response.end(JSON.stringify(result))
		}
	})
})

//前台结账
express.get("/payfor",(request,response)=>{
	var setid=url.parse(request.url,true).query.setid
	var statussql=`update setlist set status=0 where setid=${setid} and status=1`
	var statussql2=`update sumlist set status=1 where setid=${setid} and status=0`
	sql.query(statussql,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end("success")
			console.log(`${setid}号已设置为空闲状态！`)
		}
	})
	sql.query(statussql2,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end("success")
			console.log(`${setid}号付款成功，并设置为空闲状态！`)
		}
	})
})


//前台获取菜品列表
express.get("/getfoodlist",(request,response)=>{
	sql.query(`select * from foodname`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`获取菜品列表成功！`)
			response.end(JSON.stringify(result))
		}
	})
})


//前台通过菜品id获取菜品详情
express.get("/getfoodid",(request,response)=>{
	var foodid = url.parse(request.url,true).query.foodid
	sql.query(`select * from foodname where foodid=${foodid}`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`获取菜品列表成功！`)
			response.end(JSON.stringify(result))
		}
	})
})


//前台获取历史订单
express.get("/gethistoryorder",(request,response)=>{
	sql.query(`select * from sumlist where status=1`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`获取历史订单成功！`)
			response.end(JSON.stringify(result))
		}
	})
})


//前台通过菜品id更新菜品数据
express.get("/update",(request,response)=>{
	var parse = url.parse(request.url,true).query
	var foodid = parse.foodid
	var foodname = parse.foodname
	var price = parse.price
	var fenlei = parse.fenlei
	var search = parse.search
	sql.query(`update foodname set foodname='${foodname}',price=${price},fenlei=${fenlei},search='${search}' where foodid=${foodid}`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${foodname}菜品数据更新成功！`)
			response.end("success")
		}
	})
})


//前台通过id删除某个菜品
express.get("/delete",(request,response)=>{
	var foodid = url.parse(request.url,true).query.foodid
	sql.query(`delete from foodname where foodid=${foodid}`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${foodid}号菜品删除成功！`)
			response.end("success")
		}
	})
})


//前台上传菜品
express.get("/uploadname",(request,response)=>{
	var parse = url.parse(request.url,true).query
	var foodname = parse.name
	var price = parse.price
	var fenlei = parse.fenlei
	var search = parse.search
	sql.query(`insert into foodname (foodname,price,fenlei,search) values ("${foodname}",${price},${fenlei},"${search}")`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${foodname}菜品添加成功！`)
			response.end("success")
		}
	})
})

//前台新增桌号
express.get("/getsetid",(request,response)=>{
	var setid = url.parse(request.url,true).query.setid
	var select =`select * from setlist where setid=${setid}`
	var tableadd = `insert into setlist (setid) values (${setid})`
	sql.query(select,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			if(!result.length){
				sql.query(tableadd,(error,result)=>{
					if(error){
						console.log(error)
						response.end("error")
					}
					else{
						response.end("success")
						console.log(`${setid}号桌已存在！`)
					}
				})
			}
			else{
				response.end("repeat")
			}
		}
	})
})


express.listen(port)
console.log(`server is runing at ${port}`)
